﻿<?php

header("Content-type: text/html; charset=utf-8");

define('db_host', 'localhost');
define('db_user', 'root');
define('db_pass', '');
define('db_database', 'leoparder');
define('db_charset', 'utf8');

// 获取所有表名
$tables = array();
$result = sql_fetch_rows("show tables;");
foreach ($result as $r)
{
	$tname = $r["Tables_in_leoparder"];
	if (substr($tname,0,7) === "config_")
	{
		$tables[] = $tname;
	}
}

// 获取所有数据
$alldata = array();
foreach ($tables as $tname)
{
	$sql = "SELECT * FROM `" . $tname . "`";
	$result = sql_fetch_rows_2($sql);
	
	$alldata["$tname"] = $result;
}

// 序列化
if (false) // 保存为amf格式, 最终失败
{
/*
	$pack = new Amfphp_Core_Amf_Packet();
	$serializer = new Amfphp_Core_Amf_Serializer($pack);
	$serializer->writeAmf3Data($alldata);
	$confdata = $serializer->getOutput();
*/
} else	// 保存为JSON格式
{
	$confdata = json_encode($alldata);
}

// 写文件
//$confdata = gzcompress($confdata, -1);
$conffile = fopen("./assets/config.dat", "wb");
fputs($conffile, $confdata);
fclose($conffile);


// 完成
echo "数据已经刷新,请回到游戏界面刷新";



////////////////////////////////////////////////
// MySql函数
function sql_connect()
{
	$GLOBALS['db_connection'] = mysql_connect(db_host, db_user, db_pass);
	if(!$GLOBALS['db_connection']) 
	{
		file_put_contents( dirname(__FILE__)."/sql_err_log.txt"
						, "sql connection failed." .db_host . " :: " . db_user . " :: " . db_pass . "\n"
						, FILE_APPEND );
		throw new Exception(mysql_errno() . ": " . mysql_error());
	}
	mysql_set_charset(db_charset, $GLOBALS['db_connection']);
	mysql_select_db(db_database, $GLOBALS['db_connection']);
}

function sql_escape_string($buff)
{
	if(!array_key_exists('db_connection' , $GLOBALS))
	sql_connect();
	return mysql_real_escape_string($buff,$GLOBALS['db_connection']) ;
}

function sql_query($sql)
{
	if(!array_key_exists('db_connection' , $GLOBALS))
		sql_connect();

	//		file_put_contents(dirname(__FILE__)."/sql_log.txt","$sql \n",FILE_APPEND);	// 记录所有sql语句
	$r = mysql_query($sql,$GLOBALS['db_connection']);

	if($r == false)
	{
		$time = date("Ymd H:i:s") ;
		$errno=mysql_errno($GLOBALS['db_connection']);
		$errmsg=mysql_error($GLOBALS['db_connection']);
		if($errno == 2006) // mysql has gone away, 重新连接一下
		{
			sql_connect();

			$r = mysql_query($sql,$GLOBALS['db_connection']);
			if($r == false){
				$errno=mysql_errno($GLOBALS['db_connection']);
				$errmsg=mysql_error($GLOBALS['db_connection']);
				file_put_contents(dirname(__FILE__)."/error_log.txt","$time--$sql --- $errmsg(errno: $errno)\n",FILE_APPEND);
				throw new Exception("dberror -- $sql -- $errno \n");
			}
		} else if ($errno == 1213){	// 被锁
			usleep(100000) ;		// 再执行一遍(1/10秒后)
			$r = mysql_query($sql,$GLOBALS['db_connection']);
			if($r == false){
				$errno=mysql_errno($GLOBALS['db_connection']);
				$errmsg=mysql_error($GLOBALS['db_connection']);
				file_put_contents(dirname(__FILE__)."/error_log.txt","$time--$sql --- $errmsg(errno: $errno)\n",FILE_APPEND);
				throw new Exception("dberror -- $sql -- $errno \n");
			}
		} else{
			file_put_contents(dirname(__FILE__)."/error_log.txt","$time--$sql $errmsg(errno: $errno)\n",FILE_APPEND);
			throw new Exception("dberror -- $sql -- $errno \n");
		}
	}
	
	// print_r($r);
	return $r;
}

function getConnection()
{
	$conn = mysql_connect(db_host, db_user, db_pass);
	if(!$conn)
	throw new Exception(mysql_errno() . ": " . mysql_error());

	mysql_set_charset(db_charset,$conn);
	mysql_select_db(db_database, $conn);

	return $conn;
}

function sql_query_conn($conn,$sql)
{
	if(!$conn)
	{
		throw new Exception("Mysql connection interrupted");
	}

	$r = mysql_query($sql,$conn);

	if($r == false)
	{
		mysql_query("ROLLBACK",$conn);
		$errno=mysql_error($conn);
		throw new Exception("Failed on sql query.".$errno);
	}
}

// 无记录则返回
function sql_fetch_one($sql)
{
	$r = sql_query($sql);

	if(mysql_num_rows($r) == 0)
	{
		// 结果为空则返回
		mysql_free_result($r);
		return "";
	}

	if ((!empty($r))&&($row = mysql_fetch_array($r, MYSQL_ASSOC))) {
		mysql_free_result($r);
		return $row;
	}

	return mysql_error();
}

function sql_fetch_one_cell($sql)
{
	$r = sql_query($sql);

	if ((!empty($r))&&($row = mysql_fetch_array($r, MYSQL_NUM))) {
		mysql_free_result($r);
		return $row[0];
	}
	else
	{
		mysql_free_result($r);
		return false;
	}
	return 0;
}

function sql_fetch_rows_2($sql)
{
	$r = sql_query($sql);
	//echo $sql;
	
	$ret = array();
	$row = mysql_fetch_array($r,MYSQL_ASSOC);
	if ($row)
	{
		//print_r($row);
		$len = count($row);
		//echo $len;
	}
	
	while($row) {
		
//		foreach ($row as $key=>$value)
//		{
//			$type = mysql_field_type($r, $key);
//			if (stripos($type, "int") !== false)
//			{
//				$row[$key] = intval($row[$key]);
//				//echo is_int($row[$i]) ? "true" : "false";
//			}
//		}
		
		for ($i=0; $i<$len; $i++)
		{
			$type = mysql_field_type($r, $i);
			$name = mysql_field_name($r, $i);
			if (stripos($type, "int") !== false)
			{
				$row[$name] = intval($row[$name]);
				//echo is_int($row[$i]) ? "true" : "false";
			}
		}
		
		$ret[] = $row;
		$row = mysql_fetch_array($r,MYSQL_ASSOC) ;
	}
	
	mysql_free_result($r);
	return $ret;
}

function sql_fetch_rows($sql)
{
	$r = sql_query($sql);
	
	$ret = array();
	$row = mysql_fetch_array($r,MYSQL_ASSOC);
	
	while($row) {
		$ret[] = $row;
		$row = mysql_fetch_array($r,MYSQL_ASSOC) ;
	}
	
	mysql_free_result($r);
	return $ret;
}

function sql_insert($sql)
{
	sql_query($sql);
		
	return sql_fetch_one_cell('select last_insert_id()');
}

// 通过id获得一个对象
function sql_fetch_object($table, $id, $idname="id")
{
	return sql_fetch_one("SELECT * FROM `$table` WHERE `$idname`=$id");
}

// 插入一个对象
function sql_insert_object($table, $obj)
{
	if(!$obj)
	return 0;
		
	$sql = "INSERT INTO `$table` ";
	$keys = "(";
	$values = "(";
	$r = "";
	foreach ($obj as $key=>$value)
	{
		$keys .= ( $r . "`".$key."`");
		$values .= ( $r . "'" . $value . "'");
		$r = ",";
	}
	$keys .= ")";
	$values .= ")";
	$sql = $sql . $keys . " VALUES " . $values;
	return sql_insert( $sql);
}

// 替换一个对象
function sql_replace_object($table, $obj)
{
	if(!$obj)
	return 0;
		
	$sql = "REPLACE INTO $table ";
	$keys = "(";
	$values = "(";
	$r = "";
	foreach ($obj as $key=>$value)
	{
		$keys .= ( $r . "`" . $key . "`");
		$values .= ( $r . "'" . $value . "'");
		$r = ",";
	}
	$keys .= ")";
	$values .= ")";
	$sql = $sql . $keys . " VALUES " . $values;
	return sql_insert( $sql);
}

function sql_replace_objects($table, $objs)
{
	if($objs) foreach($objs as $key=>$value)
	{
		sql_replace_object($table, $value);
	}
}

function sql_check($sql)
{
	$r = sql_query($sql);

	if (empty($r)) {
		return false;
	}

	if (mysql_num_rows($r) > 0) {
		return true;
	} else {
		return false;
	}
}



?>